package openmips

import scala.io.Source
import scala.collection.mutable.ArrayBuffer

import chisel3._
import chisel3.util._

class RomSimulator {
    val instructions = ArrayBuffer[String]()

    def read(filename: String) {
        var counter = 1;
        println("Read from " + filename)
        for (line <- Source.fromFile(filename).getLines()) {
            println("Line " + counter + ": " + line)
            counter += 1
            // add to array
            instructions += line
        }
    }

    def getInst(addr: Int, enable: Boolean) : String = {
        if (enable && addr < instructions.size && addr > 0) {
            "h" + instructions(addr)
        }
        else {
            "h00000000"
        }
    }
}

// used for testing
object RomSimulator extends App {
    val sim = new RomSimulator
    sim.read("./binary/inst_rom.data")
    for (i <- 0 to 5) {
        println(sim.getInst(i, true))
    }
}